package com.ifly.voice.view;

import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

import android.R.integer;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

public class DynamicButton2 extends ImageView
{

	private static final int centreArea = 1;
	private static final int rightArea = 2;
	private static final int lefttArea = 3;
	private static final int topArea = 4;
	private static final int bottomArea = 5;
	PaintFlagsDrawFilter filter;
	private int vWidth;
	private int vHeight;
	private float coordx;
	private float coordY;
	private float scale0;
	private RectF centre;
	private Point cenPoint;
	private DynamicButton2 button;

	public DynamicButton2(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init();
	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		canvas.setDrawFilter(filter);
		super.onDraw(canvas);
	}

	private void init()
	{
		button = DynamicButton2.this;
		filter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
		;

		Drawable drawable = getDrawable();
		BitmapDrawable bd = (BitmapDrawable) drawable;
		bd.setAntiAlias(true);
		// boolean antiAlias = bd.getPaint().isAntiAlias();
		// LogUtils.d("antiAlias="+antiAlias);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		vWidth = w - getPaddingLeft() - getPaddingRight();
		vHeight = h - getPaddingTop() - getPaddingBottom();
		scale0 = (float) vHeight / vWidth;

		cenPoint = new Point(vWidth / 2, vHeight / 2);
		centre = new RectF(vWidth / 3, vHeight / 3, vWidth * 2 / 3, vHeight * 2 / 3);
	}

	int state = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		int action = event.getAction();
		if (action == MotionEvent.ACTION_DOWN)
		{
			coordx = event.getX();
			coordY = vHeight - event.getY();
			if (centre.contains(coordx, event.getY()))
			{
				state = centreArea;
			}
			else
			{
				float rx = Math.abs(coordx - vWidth / 2);
				float ry = Math.abs(coordY - vHeight / 2);
				float scale1 = ry / rx;
				if (scale1 < scale0)
				{
					if (coordx > cenPoint.x)
					{
						state = rightArea;
					}
					else
					{
						state = lefttArea;
					}
				}
				else
				{
					if (coordY > cenPoint.y)
					{
						state = topArea;
					}
					else
					{
						state = bottomArea;
					}
				}
			}
			onStateChanged(state);
			onTouchEvent.touchDown(this);
		}
		else if (action == MotionEvent.ACTION_MOVE)
		{
		}
		else if (action == MotionEvent.ACTION_UP)
		{
			onStateReset(state);
			onTouchEvent.touchUP(this);
		}
		return true;
	}

	public AnimatorManager animatorManager = new AnimatorManager();

	public AnimatorManager getAnimatorManager()
	{
		return animatorManager;
	}
	public class AnimatorManager
	{
		private static final int duration = 120;

		public AnimatorSet getRightON()
		{
			ViewHelper.setPivotX(button, 0);
			ViewHelper.setPivotY(button, vHeight / 2);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationY", 10);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getRightOFF()
		{
			ViewHelper.setPivotX(button, 0);
			ViewHelper.setPivotY(button, vHeight / 2);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationY", 0);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getLeftON()
		{
			ViewHelper.setPivotX(button, vWidth);
			ViewHelper.setPivotY(button, vHeight / 2);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationY", -10);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getLeftOFF()
		{
			ViewHelper.setPivotX(button, vWidth);
			ViewHelper.setPivotY(button, vHeight / 2);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationY", 0);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getTopON()
		{
			ViewHelper.setPivotX(button, vWidth / 2);
			ViewHelper.setPivotY(button, vHeight);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationX", 10);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getTopOFF()
		{
			ViewHelper.setPivotX(button, vWidth / 2);
			ViewHelper.setPivotY(button, vHeight);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationX", 0);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getBottomON()
		{
			ViewHelper.setPivotX(button, vWidth / 2);
			ViewHelper.setPivotY(button, 0);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationX", -10);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getBottomOFF()
		{
			ViewHelper.setPivotX(button, vWidth / 2);
			ViewHelper.setPivotY(button, 0);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "rotationX", 0);
			set.playTogether(anim);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getScaleON()
		{
			ViewHelper.setPivotX(button, cenPoint.x);
			ViewHelper.setPivotY(button, cenPoint.y);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "scaleX", 1f, 0.9f);
			ObjectAnimator anim2 = ObjectAnimator.ofFloat(button, "scaleY", 1f, 0.9f);
			set.playTogether(anim, anim2);
			set.setDuration(duration);
			return set;
		}

		public AnimatorSet getScaleOFF()
		{
			ViewHelper.setPivotX(button, cenPoint.x);
			ViewHelper.setPivotY(button, cenPoint.y);
			AnimatorSet set = new AnimatorSet();
			ObjectAnimator anim = ObjectAnimator.ofFloat(button, "scaleX", 0.9f, 1f);
			ObjectAnimator anim2 = ObjectAnimator.ofFloat(button, "scaleY", 0.9f, 1f);
			set.playTogether(anim, anim2);
			set.setDuration(duration);
			return set;
		}

	}

	Runnable rotateOn = new Runnable()
	{
		@Override
		public void run()
		{
			if (state == rightArea)
			{
				animatorManager.getRightON().start();
			}
			else if (state == lefttArea)
			{
				animatorManager.getLeftON().start();

			}
			else if (state == topArea)
			{
				animatorManager.getTopON().start();
			}
			else if (state == bottomArea)
			{
				animatorManager.getBottomON().start();
			}
		}
	};

	Runnable rotateOff = new Runnable()
	{
		@Override
		public void run()
		{
			if (state == rightArea)
			{
				animatorManager.getRightOFF().start();
			}
			else if (state == lefttArea)
			{
				animatorManager.getLeftOFF().start();
			}
			else if (state == topArea)
			{
				animatorManager.getTopOFF().start();
			}
			else if (state == bottomArea)
			{
				animatorManager.getBottomOFF().start();
			}
		}
	};

	private void onStateChanged(int state)
	{
		if (state == centreArea)
		{
			animatorManager.getScaleON().start();
		}
		else
		{
			post(rotateOn);
		}

	}

	private void onStateReset(int state)
	{
		if (state == centreArea)
		{
			animatorManager.getScaleOFF().start();
		}
		else
		{
			post(rotateOff);
		}
	}

	OnTouchEvent onTouchEvent;

	public void setOnTouchEvent(OnTouchEvent onTouchEvent)
	{
		this.onTouchEvent = onTouchEvent;
	}

	public interface OnTouchEvent
	{

		void touchDown(DynamicButton2 dynamicButton2);

		void touchUP(DynamicButton2 dynamicButton2);

	}

}
